package com.gemme.gemme.wode;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.util.DpUtil;
import com.gemme.gemme.util.MyOSSUtils;
import com.gemme.gemme.util.QRCodeUtil;
import com.gemme.gemme.util.ShareUtil;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.view.ShareDialog;
import com.gemme.gemme.view.SwitchView;
import com.gemme.gemme.view.Topview;
import com.gemme.gemme.view.TuijianguanDialog;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;
import com.sina.weibo.sdk.share.ShareUtils;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//侧滑删除地址
public class Tuijianguan2Activity extends BaseActivity{
    boolean isTjg = false;
    View ll_tuijian;
    View ll_to_tuijian;
    TextView tv_guize;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tuijiaguan2);
        initTop();
        initView();
        getCode();
    }
    SwitchView switchview;
    TextView tv_code;
    ImageView iv_qrcode;
    TextView tv_zhuanfa;
    private void initView() {
//        createQRCodeBitmap("你好呀", 800, 800,"UTF-8","H", "1", Color.GREEN, Color.WHITE);

//        Bitmap bitmap = QRCodeUtil.createQRCodeBitmap("你好呀", 800, 800);
        ll_tuijian = findViewById(R.id.ll_tuijian);
        ll_to_tuijian = findViewById(R.id.ll_to_tuijian);
        switchview = findViewById(R.id.switchview);

        tv_guize = findViewById(R.id.tv_guize);


        tv_code = findViewById(R.id.tv_code);
        iv_qrcode = findViewById(R.id.iv_qrcode);
        tv_zhuanfa = findViewById(R.id.tv_zhuanfa);

        changeState();

        switchview.setOnSwitchChange(new SwitchView.OnSwitchChange() {
            @Override
            public void OnSwitchChange(boolean isOn) {
                isTjg = isOn;
                if(isOn){
                    toTuijianguan();
                }
                changeState();
            }
        });
        tv_guize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TuijianguanDialog tuijianguanDialog = new TuijianguanDialog();
                tuijianguanDialog.setUrl("http://47.114.162.17/protocol/index.html?type=protoco3");
                tuijianguanDialog.showDialog(mContext);
            }
        });

        tv_zhuanfa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bitmap != null){
//                    QRCodeUtil.shareImg(bitmap,mContext);
                    share(bitmap);
                }else{
                    Toast.makeText(mContext, "您还不是推荐官", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }



    Bitmap bitmap;
    private void getCode() {
        String url = DataManager.BaseUrl + "user/recommend/findinvite" ;
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();

        if(TextUtils.isEmpty(access_token))
            return;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,"Bearer " + access_token);
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    System.out.println("");
                    String code = (new JSONObject(response)).getString("data");
                    if (TextUtils.isEmpty(StrUtil.noNUll(code)))
                        return;
                    tv_code.setText(code);

                    bitmap = QRCodeUtil.createQRCodeBitmap(code, DpUtil.dp2px(mContext,120), DpUtil.dp2px(mContext,120));
                    iv_qrcode.setImageBitmap(bitmap);
                    switchview.setState(true);
                    switchview.noCan();
                }catch (Exception e){

                }
            }
        });
    }



    private void toTuijianguan() {
        String url = DataManager.BaseUrl + "user/recommend/saverecommend";
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();

        if(TextUtils.isEmpty(access_token))
            return;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,"Bearer " + access_token);
        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            DataManager.refreshToken(mContext);
                            getCode();
                        }
                    }).start();

                    /*System.out.println("");
                    if(DataPares.isSucess(mContext,response)){

                        String code = (new JSONObject(response)).getString("data");
                        if (TextUtils.isEmpty(code))
                            return;
                        switchview.setState(true);
                        tv_code.setText(code);
                        bitmap = QRCodeUtil.createQRCodeBitmap(code, DpUtil.dp2px(mContext,120), DpUtil.dp2px(mContext,120));
                        iv_qrcode.setImageBitmap(bitmap);
                        Toast.makeText(mContext, "成为推荐官成功", Toast.LENGTH_SHORT).show();
                        switchview.noCan();
                    }else{
                        Toast.makeText(mContext, "成为推荐官失败", Toast.LENGTH_SHORT).show();
                    }*/



                }catch (Exception e){

                }
            }
        });
    }


    private void changeState() {
        /*if(isTjg){
            ll_tuijian.setVisibility(View.VISIBLE);
            ll_to_tuijian.setVisibility(View.GONE);
        }else{
            ll_tuijian.setVisibility(View.GONE);
            ll_to_tuijian.setVisibility(View.VISIBLE);
        }*/
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("推荐官");

    }

    private void share(Bitmap bitmap) {
        try {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    File file = ShareUtil.saveImageToGallery(mContext, bitmap, "fenxiangma");
                    if(file != null && file.exists()){
                        MyOSSUtils.getInstance().upImage1(mContext, file.getPath(), new MyOSSUtils.OssUpCallback() {
                            @Override
                            public void successImg(String img_url) {
                                if(TextUtils.isEmpty(img_url))
                                    return;
                               /* ShareUtil.shareWinxin(mContext,img_url,"http://www.ahkuayu.com/",
                                        "集美美妆注册邀请码",tv_code.getText() + "");*/
                               runOnUiThread(new Runnable() {
                                   @Override
                                   public void run() {
                                       ShareDialog shareDialog = new ShareDialog();
                                       shareDialog.showDialog(mContext);
                                       shareDialog.setData(img_url,"http://www.ahkuayu.com/",
                                               "集美美妆注册邀请码" + "",tv_code.getText() + "");
                                   }
                               });


                            }

                            @Override
                            public void successVideo(String video_url) {
                                System.out.println("");
                            }

                            @Override
                            public void inProgress(long progress, long zong) {
                                System.out.println("");
                            }
                        });
                    }

                }
            }).start();

        }catch (Exception e){

        }
    }



}